package com.elong.common.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class ThriftClient {

    public static void main(String[] args) {
        try{
            TTransport transport;
            boolean flag = true;
            if(flag){
                transport = new TSocket("127.0.0.1",9090);
                transport.open();
            }else{
                /*
                 * Similar to the server, you can use the parameters to setup client parameters or
                 * use the default settings. On the client side, you will need a TrustStore which
                 * contains the trusted certificate along with the public key. 
                 * For this example it's a self-signed cert. 
                 */
                TSSLTransportParameters params = new TSSLTransportParameters();
                /*
                 * Get a client transport instead of a server transport. The connection is opened on
                 * invocation of the factory method, no need to specifically call open()
                 */
                transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params);
            }
            TProtocol protocol = new TBinaryProtocol(transport);
            SharedService.Client client = new SharedService.Client(protocol);
            perform(client);
            transport.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
    
    private static void perform(SharedService.Client client) throws Exception{
        RequestStruct request = new RequestStruct();
        request.setRequestId(333);
        request.setRequestData(3);
        client.send_SendReceive(request);
        System.out.println("send ok");
    }
    
}
